package com.pybox.service;

import com.pybox.dto.CabinetObjectsDto;
import com.pybox.dto.TCabinetsDto;
import com.pybox.dto.TObjectsCabinetDto;
import com.pybox.dto.TObjectsDto;
import com.pybox.entity.TCabinets;
import com.pybox.entity.TObjects;
import com.pybox.entity.TObjectsCabinet;
import com.pybox.repository.TCabinetsRepository;
import com.pybox.repository.TObjectsCabinetRepository;
import com.pybox.repository.TObjectsRepository;
import com.pybox.spec.TObjectsCabinetSpec;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* 描述：TObjectsCabinet Service
* @author wuyingkai
* @date 2018-09-15
*/

@Service
@Transactional
public class TObjectsCabinetService {

    @Autowired
    private TObjectsCabinetRepository repository;

    @Autowired
    private TCabinetsRepository tCabinetsRepository;

    @Autowired
    private TObjectsRepository tObjectsRepository;
    /**
     * 创建TObjectsCabinet
     *
     * @param source
     * @return
     */
    public boolean create(TObjectsCabinetDto source){

        TObjectsCabinet target = new TObjectsCabinet();
        BeanUtils.copyProperties(source, target);
        repository.save(target);

        return true;

    }

    /**
     * 根据ID删除TObjectsCabinet
     *
     * @param id
     * @return
     */
    public boolean delete(Integer id){

        repository.delete(id);

        return true;

    }

    /**
     * 更新TObjectsCabinet
     *
     * @param id
     * @param source
     * @return
     */
    public boolean update(Integer id, TObjectsCabinetDto source){

        TObjectsCabinet exist = repository.findOne(id);
        if (null == exist){
            throw new RuntimeException("记录不存在");
        }

        BeanUtils.copyProperties(source, exist, new String[]{"id"});
        repository.save(exist);

        return true;

    }

    /**
     * 根据ID查询TObjectsCabinet
     *
     * @param uuid
     * @return
     */
    public CabinetObjectsDto listObjectsOfCabinet(String uuid){
        CabinetObjectsDto dto = new CabinetObjectsDto();
        TCabinets exist = tCabinetsRepository.getTCabinetsByUuid(uuid);
        if (null == exist){
            throw new RuntimeException("记录不存在");
        }
        TCabinetsDto target = new TCabinetsDto();
        BeanUtils.copyProperties(exist, target);
        dto.setTCabinetsDto(target);
//        List<TObjectsCabinet> list = repository.findAllByCabinetUUId(uuid);
        List<TObjects> objectsList = new ArrayList<>();
//        TObjects objects = new TObjects();
        objectsList = tObjectsRepository.findBycabinetUid(uuid);
//        for(TObjectsCabinet objectsCabinet:list){
//            objects = tObjectsRepository.findByUuid(objectsCabinet.getUkeyUuid());
//            if (objects != null){
//                objectsList.add(objects);
//            }
//        }
        dto.setTObjectsList(objectsList);
        return dto;

    }


    /**
     * 根据ID查询TObjectsCabinet
     *
     * @param id
     * @return
     */
    public TObjectsCabinetDto detail(Integer id){

        TObjectsCabinet exist = repository.findOne(id);
        if (null == exist){
            throw new RuntimeException("记录不存在");
        }

        TObjectsCabinetDto target = new TObjectsCabinetDto();
        BeanUtils.copyProperties(exist, target);

        return target;

    }
    /**
     * 分页查询查询TObjectsCabinet
     *
     * @param pageNo
     * @param pageSize
     * @param paramMap
     * @return
     */
    public Page<TObjectsCabinetDto> page(Integer pageNo, Integer pageSize, Map<String, String> paramMap){

        Pageable pageable = new PageRequest(pageNo - 1, pageSize);
        Page<TObjectsCabinet> data = repository.findAll(new TObjectsCabinetSpec(paramMap), pageable);

        List<TObjectsCabinetDto> list = new ArrayList<>();
        for (TObjectsCabinet obj : data.getContent()) {
            TObjectsCabinetDto dto = new TObjectsCabinetDto();
            BeanUtils.copyProperties(obj, dto);
            list.add(dto);
        }

        Page<TObjectsCabinetDto> page = new PageImpl<>(list, pageable, data.getTotalElements());

        return page;

    }
}